
import { postRequest, getRequest, postRequestBlob, postObjectReturnBlob, postObjRequest, postMultipartRequest } from "./request";
import { urlMap } from "./urls";

export function post(url: string, data = {}) {
    return new Promise((resolve, reject) => {
        postRequest(urlMap[url], data).then(res => {
            resolve(res);
        }).catch(res => {
            reject(res);
        });
    });
}

//参数是object
export function postObj(url: string, data = {}) {
    return new Promise((resolve, reject) => {
        postObjRequest(urlMap[url], data).then(res => {
            resolve(res);
        }).catch(res => {
            reject(res);
        });
    });
}

//Content-Type:multipart/form-data
export function postMultipart(url: string, data = {}) {
    return new Promise((resolve, reject) => {
        postMultipartRequest(urlMap[url], data).then(res => {
            resolve(res);
        }).catch(res => {
            reject(res);
        });
    });
}

export function get(url: string, data = {}, isUrl = false) {
    return new Promise((resolve, reject) => {
        let link;
        if (!isUrl) link = urlMap[url];
        else link = url;
        getRequest(link, {
            params: data
        }).then(res => {
            resolve(res);
        }).catch(res => {
            reject(res);
        });
    });
}


//blob文件格式
export function postBlob(url: string, data = {}) {
    return new Promise((resolve, reject) => {
        postRequestBlob(urlMap[url], data).then(res => {
            resolve(res);
        }).catch(res => {
            reject(res);
        });
    });
}


//参数是object,返回blob文件格式
export function postObjectBlob(url: string, data = {}) {
    return new Promise((resolve, reject) => {
        postObjectReturnBlob(urlMap[url], data).then(res => {
            resolve(res);
        }).catch(res => {
            reject(res);
        });
    });
}

export function myToken() {
    return localStorage.getItem('token') || ''
}